package com.study.basetype.spring;

import com.study.basetype.annotation.Love;
import com.study.basetype.service.ILoveService;
import com.study.basetype.service.LoveFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

/**
 * @author flwu
 * @date 2020/3/22 3:47 PM
 * @MOTTO 保持空杯和敬畏
 */
@Component
public class LoveAnnotatioScanner implements BeanPostProcessor {

    @Nullable
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Nullable
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof ILoveService) {
            Love love = bean.getClass().getAnnotation(Love.class);
            if (love != null) {
                LoveFactory.addService(love.type().getType(), (ILoveService) bean);
            }
        }
        return bean;
    }
}
